package com.android.quiz;

import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class Remainder extends Activity {
	
	
	private static int HELLO_ID =1;
	Button setdate, settime, confirm;
	TextView date, time, setcontent;
	private int mYear;
    private int mMonth;
    private int mDay;
    private int mHour;
    private int mMinute;
    private static final int DATE_DIALOG_ID = 0;
    static final int TIME_DIALOG_ID = 1;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.remainder);
        
        Initialise();
        
    // A listener for the set date button
        setdate.setOnClickListener(new OnClickListener() {  public void onClick(View v) {  showDialog(DATE_DIALOG_ID);  }  });
        
    // A listener for the set time button
        settime.setOnClickListener(new OnClickListener() {  public void onClick(View v) {  showDialog(TIME_DIALOG_ID);  }  });
        
    // A listener for the confirm button
        confirm.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				Calendar cal = Calendar.getInstance();
		        
				// Add minutes to the calendar object
				cal.set(Calendar.MONTH, mMonth);
				cal.set(Calendar.YEAR, mYear);				
				cal.set(Calendar.DAY_OF_MONTH, mDay);
				cal.set(Calendar.HOUR_OF_DAY, mHour);
				cal.set(Calendar.MINUTE, mMinute);
				
				Intent alarmintent = new Intent(getApplicationContext(), AlarmReceiver.class);
				alarmintent.putExtra("title","Quiz Master");
				alarmintent.putExtra("note","Test Scheduled!");

				PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), HELLO_ID, alarmintent,PendingIntent.FLAG_ONE_SHOT|  Intent.FILL_IN_DATA);
				
				AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
				am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
				
				Toast.makeText(getApplicationContext(), "Alarm set at " + mYear + "-" +  ++mMonth + "-" + mDay + "---" + "--" + mHour + ":" + mMinute+ "!.", Toast.LENGTH_LONG).show();
				finish();
			}
		});
        
        }
	
	public void Initialise(){
		setdate = (Button) findViewById(R.id.Setdate);
		settime =  (Button) findViewById(R.id.settime);
		confirm = (Button) findViewById(R.id.confirm);
		
		date = (TextView) findViewById(R.id.ShowDate);
		time = (TextView) findViewById(R.id.ShowTime);
	}
	
	public void updateDisplay(){
    	date.setText(
                new StringBuilder()
                        // Month is 0 based so add 1
                        .append(mMonth + 1).append("-")
                        .append(mDay).append("-")
                        .append(mYear).append(" "));
    }

	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
    	
    	public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    		mYear = year;
    		mMonth = monthOfYear;
    		mDay = dayOfMonth;
    		updateDisplay();
    		}
    	};
    	
    	@Override
    	protected Dialog onCreateDialog(int id) {
    		switch (id) {
    		case DATE_DIALOG_ID: return new DatePickerDialog(this,mDateSetListener, mYear, mMonth, mDay);
    		case TIME_DIALOG_ID: return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, false);
    			}
    		return null;
    		
		}
    	
    	private void updateTime() {
            time.setText(
                new StringBuilder()
                        .append(pad(mHour)).append(":")
                        .append(pad(mMinute)));
        }
    	
    	private static String pad(int c) {
            if (c >= 10)
                return String.valueOf(c);
            else
                return "0" + String.valueOf(c);
        }
    	
    	private TimePickerDialog.OnTimeSetListener mTimeSetListener =
    	        new TimePickerDialog.OnTimeSetListener() {
    	            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    	                mHour = hourOfDay;
    	                mMinute = minute;
    	                updateTime();
    	            }
    	        };
	
}
